package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.UserService;
import com.njtech.pet.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:gm
 * Date:2020-03-22 18:29
 * Description:<描述>用户管理员的工作
 * @author gmisa
 */
@Controller
@RequestMapping("/userManager")
public class UserManagerController {

    @Autowired
    private UserService userService;

    /**
     * 功能1：管理员注销、冻结、恢复用户:
     * @param username  用户名
     * @param temp      情况（0，1，2）
     * @return          提示信息
     */
    @RequestMapping(value={"/changeState"})
    @ResponseBody
    public  String logoff(@RequestParam String username,@RequestParam String temp) {
        Map<String,Object> resultMap = new HashMap<>();
        System.out.println("uid="+username+"temp="+temp);
        if(!username.equals("")&&username!=null && (!temp.equals("")) && temp!=null){
            List<User> user = userService.selectByUserNameAndState(username,null);
            if(user.size()!=0) {
                switch (temp){
                    case "0":
                        user.get(0).setState(Constant.User.STATE_INVALID.getValue());
                        break;
                    case "1":
                        user.get(0).setState(Constant.User.STATE_FREEZE.getValue());
                        break;
                    case "2":
                        user.get(0).setState(Constant.User.STATE_VALID.getValue());
                        break;
                    default:
                }
                int flag = userService.updateUser(user.get(0));
                if(flag==1){
                    resultMap.put("msg","更新成功");
                }else{
                    resultMap.put("msg","更新失败");
                }
            }else{
                resultMap.put("msg","用户不存在！");
            }
        }else{
            resultMap.put("msg","uid或temp不能为空！");
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * @param uid               用户id
     * @param username          用户名
     * @param sex               性别
     * @param role              角色
     * @param state             状态
     * @param beginCreateDate  开始创建日期
     * @param endCreateDate    结束创建日期
     * @param beginUpdateDate  开始更改日期
     * @param endUpdateDate    结束更改日期
     * @return  结果集
     */
    @RequestMapping("/selectElasticly")
    @ResponseBody
    public String selectElasticly(Integer uid,String username,String sex,String role,String state, String beginCreateDate, String endCreateDate, String beginUpdateDate, String endUpdateDate){
        List<User>userList = userService.selectElasticly(uid,username,sex,role,state,beginCreateDate,endCreateDate,beginUpdateDate,endUpdateDate);
        return JSON.toJSONString(userList);
    }

    /**
     * 添加用户
     * @param username   用户名
     * @param pwd        密码
     * @param pwdAgain   确认密码
     * @param sex        性别
     * @param role       角色
     * @return           提示信息
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(String username,String pwd,String pwdAgain,String sex,String role){
        User user=new User();
        Map<String,Object> resultMap = new HashMap<>();
        if(username.equals("")||username==null||
                pwd.equals("")||pwd==null||
                pwdAgain.equals("")||pwdAgain==null||
                sex.equals("")||sex==null||
                role.equals("")||role==null||
                sex.equals("")||sex==null){
            resultMap.put("msg","不能有非空字段！");
            return JSON.toJSONString(resultMap);
        }
        if(!pwd.equals(pwdAgain)){
            resultMap.put("msg","两次密码不相同!");
        }else {
            //插入一条数据
            user.setUsername(username);
            user.setPwd(Md5Util.MD5EncodeUtf8(pwd));
            user.setHeadUrl("0.jpg");
            user.setCredit(0);
            user.setLevel(0);
            user.setSex(sex);
            user.setState(Constant.User.STATE_VALID.getValue());
            user.setRole(role);
            String msg  = userService.insert(user);
            resultMap.put("msg", msg);
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 根据iD找用户
     * @param uid  用户id
     * @return     user
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public String selectById(Integer uid){
        Map<String,Object> resultMap = new HashMap<>();
        User user = userService.selectByUserId(uid);
        resultMap.put("user",user);
        return JSON.toJSONString(user);
    }

    /*******************
     * 功能5：退出登录
     * @param session
     * @return
     * -------------成功
     */
    @RequestMapping(value={"/logout"},method = RequestMethod.POST)
    @ResponseBody
    public static  String logout(HttpSession session) {
        session.removeAttribute("user");
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("msg","已退出");
        return JSON.toJSONString(resultMap);
    }
}
